package Lee_code;

import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author blh
 * @date 2024年07月01日 19:26
 */
public class Lee_128_71 {
    public int longestConsecutive(int[] nums) {
        Set<Integer> hashSet = new HashSet<>();
        // 使用桶排序不行,因为不合适
        for (int num : nums) {
            hashSet.add(num);
        }
        int maxLen = 0;
        for (int i : nums) {
            if (!hashSet.contains(i - 1)) {
                int currentNum = i;
                int curLen = 1;
                while (hashSet.contains(currentNum + 1)) {
                    currentNum++;
                    curLen++;
                }
                maxLen = Math.max(maxLen, curLen);
            }
        }
        return maxLen;
    }
}
